﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Serialization;

[Serializable]
public class Asset
{
    [CompilerGenerated]
    private static Dictionary<string, int> <>f__switch$map1;
    public string dataFileName;
    public string extension;
    public string guid;
    [SerializeField]
    internal TextAsset internalTextAsset;
    [SerializeField]
    internal bool isInternalAsset;
    [SerializeField]
    private GameObject m_gameObject;
    public string modelFileName;
    public string name;
    [FormerlySerializedAs("Object")]
    public UnityEngine.Object objectReference;

    public T GetComponent<T>() where T: Component
    {
        return ((this.gameObject == null) ? null : this.gameObject.GetComponent<T>());
    }

    public string LoadDataText()
    {
        if (this.internalTextAsset != null)
        {
            return this.internalTextAsset.text;
        }
        if (!File.Exists(this.dataFileName))
        {
            object[] args = new object[] { this.dataFileName, this.nameWithExtension };
            Debug.LogErrorFormat("Could not load data, file '{0}' does not exists. (at {1})", args);
            return null;
        }
        return File.ReadAllText(this.dataFileName);
    }

    private GameObject LoadPrefab()
    {
        if (this.extension != AssetManager.prefabExtension)
        {
            return null;
        }
        GameObject cache = AssetManager.GetCache<GameObject>(this.name);
        if (cache == null)
        {
            cache = new GameObject(this.name) {
                transform = { parent = AssetManager.prefabPool.transform }
            };
            cache.gameObject.AddComponent<AssetReference>().assetName = this.name;
            Model objectToOverwrite = cache.AddComponent<Model>();
            objectToOverwrite.prefabFileName = this.modelFileName;
            if (AssetManager.IsEnvironmentObject(this.name))
            {
                cache.isStatic = true;
            }
            if (AssetManager.IsEnvironmentObject(this.name))
            {
                cache.GetOrAddComponent<ModelEmission>();
            }
            JObject obj3 = AssetManager.GetCache<JObject>(this.dataFileName);
            if (obj3 == null)
            {
                try
                {
                    string json = this.LoadDataText();
                    if (json != null)
                    {
                        obj3 = JObject.Parse(json);
                    }
                }
                catch (Exception exception)
                {
                    object[] args = new object[] { exception.Message, this.nameWithExtension };
                    Debug.LogErrorFormat("Failed to load data : {0} (at '{1}')", args);
                }
            }
            if (obj3 != null)
            {
                AssetManager.SetCache(this.dataFileName, obj3);
                IEnumerator<JProperty> enumerator = obj3.Children().Cast<JProperty>().GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        JProperty current = enumerator.Current;
                        string name = current.Name;
                        JToken token = current.Value;
                        string key = name;
                        if (key != null)
                        {
                            int num;
                            if (<>f__switch$map1 == null)
                            {
                                Dictionary<string, int> dictionary = new Dictionary<string, int>(2);
                                dictionary.Add("GameObject", 0);
                                dictionary.Add("Model", 1);
                                <>f__switch$map1 = dictionary;
                            }
                            if (<>f__switch$map1.TryGetValue(key, out num))
                            {
                                if (num == 0)
                                {
                                    if (token["layerName"] != null)
                                    {
                                        cache.layer = LayerMask.NameToLayer(token["layerName"].ToString());
                                    }
                                    continue;
                                }
                                if (num == 1)
                                {
                                    JsonUtility.FromJsonOverwrite(token.ToString(), objectToOverwrite);
                                    continue;
                                }
                            }
                        }
                        if (name == "$shortName")
                        {
                            name = this.shortName;
                        }
                        System.Type type = AssetManager.FindType(this.author, name);
                        if (type == null)
                        {
                            object[] objArray2 = new object[] { name, this.nameWithExtension };
                            Debug.LogErrorFormat("Could not find type '{0}' (at '{1}')", objArray2);
                        }
                        else
                        {
                            Component target = cache.GetComponent(type);
                            if (target == null)
                            {
                                target = cache.AddComponent(type);
                            }
                            GameJsonUtility.FromJsonOverwrite(token, target);
                            if (target is IScriptedPrefab)
                            {
                                (target as IScriptedPrefab).InitPrefab();
                            }
                        }
                    }
                }
                finally
                {
                    if (enumerator == null)
                    {
                    }
                    enumerator.Dispose();
                }
            }
            AssetManager.SetCache(this.name, cache);
        }
        return cache;
    }

    public void SaveDataText(string text)
    {
        if (this.internalTextAsset == null)
        {
            File.WriteAllText(this.dataFileName, text);
        }
    }

    public string author
    {
        get
        {
            char[] separator = new char[] { '/' };
            return this.name.Split(separator).FirstOrDefault<string>();
        }
    }

    public string category
    {
        get
        {
            return Path.GetDirectoryName(this.name);
        }
    }

    public string categoryWithoutAuthor
    {
        get
        {
            char[] separator = new char[] { '/' };
            return string.Join("/", this.category.Split(separator).Skip<string>(1).ToArray<string>());
        }
    }

    public GameObject gameObject
    {
        get
        {
            return ((this.m_gameObject == null) ? (this.m_gameObject = this.LoadPrefab()) : this.m_gameObject);
        }
        set
        {
            this.m_gameObject = value;
        }
    }

    public bool isBuiltinAsset
    {
        get
        {
            return (this.author == "__Builtin");
        }
    }

    public string localizedName
    {
        get
        {
            return this.shortName.Localize(this.name);
        }
    }

    public GameObject modelGameObject { get; private set; }

    public string nameWithExtension
    {
        get
        {
            return (this.name + this.extension);
        }
    }

    public string nameWithoutAuthor
    {
        get
        {
            char[] separator = new char[] { '/' };
            return string.Join("/", this.name.Split(separator).Skip<string>(1).ToArray<string>());
        }
    }

    public string shortName
    {
        get
        {
            return Path.GetFileNameWithoutExtension(this.name);
        }
    }
}

